package be.idamf.mx.rpc
{
	import flash.events.IEventDispatcher;
	import flash.net.FileReference;
	
	import mx.rpc.events.ResultEvent;
	
	[DefaultProperty("service")]

	public class ResultEventRecorder
	{
		////////////////////////////////////////////////////////////////////////
		
		public var data:Object;

		////////////////////////////////////////////////////////////////////////
		
		public function ResultEventRecorder(service:IEventDispatcher = null, data:Object = null)
		{
			super();
			this.service = service;
			this.data = data;
		}
		
		////////////////////////////////////////////////////////////////////////
		
		private var _service:IEventDispatcher;
		[Bindable]
		public function get service():IEventDispatcher
		{
			return _service;
		}
		public function set service(value:IEventDispatcher):void
		{
			if (_service)
				_service.removeEventListener(ResultEvent.RESULT, resultHandler);
			
			_service = value;
			
			if (_service)
				_service.addEventListener(ResultEvent.RESULT, resultHandler);
		}
		
		////////////////////////////////////////////////////////////////////////
		
		protected function resultHandler(event:ResultEvent):void
		{
			var destination:String;
			var operation:String;
			destination = event.message.destination;
			operation = event.message["operation"];
			//var args:Array = event.message.body;
			
			data[destination + "/" + operation] = event.result;
		}
	}
}